package com.hzit.rocket.listener;

import com.alibaba.fastjson.JSON;
import com.hzit.thirdparty.util.SmsUtil;
import lombok.SneakyThrows;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Component
@RocketMQMessageListener(consumerGroup = "hzit-rocketmqconsumer-group", topic = "hzit-topic")
public class MyMQSMSListener implements RocketMQListener<Map> {
	@Autowired
	private SmsUtil smsUtil;
	@SneakyThrows
	@Override
	public void onMessage(Map message) {
		System.out.println("message = " + message);
		//1. 借助阿里云平台发送手机验证码到指定的手机号上
		//1.1 得到手机号
		String phone = (String) message.get("phone");
		//1.2 得到验证码
		String code = (String) message.get("code");
		//1.3 组织成模板参数
		Map<String,String> map = new HashMap<>();
		map.put("code",code);
		//1.4 调用阿里云平台发送消息
		smsUtil.sendSms(phone, JSON.toJSONString(map));  // {"code":"123343"}
		System.out.println("消息发送成功！");
	}
}
